package com.qf.fmall2203.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall2203.product.entity.Product;
import com.qf.fmall2203.product.entity.ProductImg;
import com.qf.fmall2203.product.entity.ProductSku;
import com.qf.fmall2203.product.mapper.ProductMapper;
import com.qf.fmall2203.product.service.IProductImgService;
import com.qf.fmall2203.product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2203.product.service.IProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author dongjl
 * @since 2023-02-15
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    IProductImgService productImgService;

    @Autowired
    IProductSkuService productSkuService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Product> listRecommends() {

        //1. 按照上架时间倒叙，查出三个商品的基本信息
        final List<Product> productList = list(new QueryWrapper<Product>().orderByDesc("create_time").last("limit 3"));

        //2. 填充商品图片信息
        populateImg(productList);

        return productList;
    }

    /**
     * 为指定的商品list中的商品添加对应的图片信息
     * @param productList
     */
    public void populateImg(List<Product> productList) {

        //1. List<Product>  ---->  List<ID>
        final List<String> idList = productList.stream().map(Product::getProductId).collect(Collectors.toList());

        //2. 查询商品图片信息
        final List<ProductImg> imgList = productImgService.list(new QueryWrapper<ProductImg>().in("item_id", idList));

        //3. List<ProductImg>  ---->  Map<ItemId,List<ProductImg>>  使用 groupby
        final Map<String, List<ProductImg>> imgMap = imgList.stream().collect(Collectors.groupingBy(ProductImg::getItemId));

        //4. 把图片信息挂到商品对象中
        productList.forEach(product -> {
            final String productId = product.getProductId();
            List<ProductImg> productImgs = imgMap.get(productId);
            if (productImgs==null){
                // 商品没有关联的图片信息 ，为了给前端返回 [],而不是返回null
                productImgs = new ArrayList<>();
            }
            product.setImgs(productImgs);
        });


    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Map<String, Object> detailInfo(Integer id) {

        //1. 查询商品信息
        final Product productInfo = getById(id);

        //2. 查询商品套餐信息
        final List<ProductSku> skuList = productSkuService.list(new QueryWrapper<ProductSku>().eq("product_id", id));

        //3. 查询商品图片信息
        final List<ProductImg> imgList = productImgService.list(new QueryWrapper<ProductImg>().eq("item_id", id));

        final HashMap<String, Object> data = new HashMap<>();

        data.put("product",productInfo);
        data.put("productSkus",skuList);
        data.put("productImgs",imgList);

        return data;
    }
}
